package com.dreem.principle.segregation.improve;

/**
 * @program: DesignParrtern
 * @description:
 * @author: Peng
 * @create: 2021-01-14  23:22
 **/
public class InterfaceSegregation2 {
    public static void main(String[] args){
        A a = new A();
        a.depend1(new B());
        a.depend2(new B());
        a.depend3(new B());

        C c = new C();
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

// 定义一个接口，接口中存在五个方法
interface Interface1{
    void operation1();
}

interface Interface2{
    void operation2();
    void operation3();
}

interface Interface3{
    void operation4();
    void operation5();
}

// 类B实现了接口Interface1
class B implements Interface2, Interface1 {

    @Override
    public void operation1() {
        System.out.println("B 实现了Interface1中的operation1");
    }

    @Override
    public void operation2() {
        System.out.println("B 实现了Interface2中的operation2");
    }

    @Override
    public void operation3() {
        System.out.println("B 实现了Interface2中的operation3");
    }

}

// 类D实现了接口Interface1
class D implements Interface3, Interface1 {

    @Override
    public void operation1() {
        System.out.println("D 实现了Interface1中的operation1");
    }

    @Override
    public void operation4() {
        System.out.println("D 实现了Interface3中的operation4");
    }

    @Override
    public void operation5() {
        System.out.println("D 实现了Interface3中的operation5");
    }
}

// 类A通过接口Interface1来依赖（使用）类B
class A{
    public void depend1(Interface1 interface1){
        interface1.operation1();
    }
    public void depend2(Interface2 interface1){
        interface1.operation2();
    }
    public void depend3(Interface2 interface1){
        interface1.operation3();
    }
}

// 类C通过接口Interface1来依赖（使用）类D
class C{
    public void depend1(Interface1 interface1){
        interface1.operation1();
    }
    public void depend4(Interface3 interface1){
        interface1.operation4();
    }
    public void depend5(Interface3 interface1){
        interface1.operation5();
    }
}
